/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agenda.calendario;

import agenda.calendario.DAL.CompromissoDAL;
import agenda.calendario.DAL.ContatoDAL;
import framework.agendaFactory.BasicAgendaFactory;
import framework.groups.GroupStrategy;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.InternalFrameEvent;

/**
 *
 * @author Sergio Carlos Peres
 */
public class CompromissoFactory extends BasicAgendaFactory {
    
    private String sql = "SELECT * FROM item WHERE status = 'F' AND datahora >= '" + framework.utilitarios.Data.retona() + "' ORDER BY datahora";
    
    public int codigo;
    public javax.swing.JTable tabela = new javax.swing.JTable();
    public javax.swing.JTable tabelaCategoria = new javax.swing.JTable();
    public javax.swing.JTextField txtCompromisso;
    public javax.swing.JTextField txtEndereco;
    public javax.swing.JFormattedTextField txtData;
    public javax.swing.JFormattedTextField txtHora;
    public javax.swing.JEditorPane txtDescricao;
    public javax.swing.JCheckBox chbContato;
    public javax.swing.JComboBox cboContato;
    public java.text.SimpleDateFormat horaFormatada = new java.text.SimpleDateFormat("HH:mm");
    public com.toedter.calendar.JDateChooser dtCalendario;
    public javax.swing.ButtonGroup groupVisualizar;
    public javax.swing.JRadioButton rbnConcluido;
    public javax.swing.JRadioButton rbnDia;
    public javax.swing.JRadioButton rbnNaoConcluido;
    public javax.swing.JRadioButton rbnSeleciona;
    
    @Override
    public void jInternalFrameOpened(InternalFrameEvent evt) {
        this.controle = true;
        preencheComboBox("SELECT nome FROM contato");
        preencheTabela(sql);
        setParametroConexao();
    }
    
    @Override
    public void preencheTabela(String sql) {
        tabela.getColumnModel().getColumn(0).setPreferredWidth(25);
        tabela.getColumnModel().getColumn(1).setPreferredWidth(200);
        tabela.getColumnModel().getColumn(2).setPreferredWidth(200);
        tabela.getColumnModel().getColumn(3).setPreferredWidth(150);
        tabela.getColumnModel().getColumn(4).setPreferredWidth(150);
        tabela.getColumnModel().getColumn(5).setPreferredWidth(150);
        
        javax.swing.table.DefaultTableModel modelo = (javax.swing.table.DefaultTableModel) tabela.getModel();
        modelo.setNumRows(0);
        
        framework.dal.Conexao conAux = new framework.dal.Conexao();
        framework.dal.Conexao conexao = new framework.dal.Conexao();
        conAux.conecta();
        conexao.conecta();
        try {
            conexao.executeSelect(sql);
            java.sql.Date data;
            while (conexao.resultset.next()) {
                // Trata contato
                data = conexao.resultset.getDate("datahora");
                String contato = conexao.resultset.getString("idcontato");
                if (contato == null) {
                    contato = "";
                } else {
                    conAux.executeSelect("SELECT nome FROM contato WHERE idcontato = " + contato);
                    conAux.resultset.next();
                    contato = conAux.resultset.getString("nome");
                }
                // Trata Local
                String local = conexao.resultset.getString("endereco");
                if (local == null) {
                    local = "não especificado!";
                }
                // Add Tabela
                modelo.addRow(new Object[]{conexao.resultset.getString("status").equals("T"), conexao.resultset.getString("compromisso"),
                    framework.utilitarios.Data.retornaStirng(data),
                    horaFormatada.format(conexao.resultset.getTime("datahora")),
                    local,
                    contato
                });
            }
        } catch (java.sql.SQLException ex) {
            System.out.println("Pau...\nColocar mensagem de erro em: preencheTabela() de CompromissoFectory");
        }
        conAux.desconecta();
        conexao.desconecta();
        preecheTabelaCategoria();
    }
    
    @Override
    public void preencheComboBox(String sql) {
        framework.dal.Conexao conexao = new framework.dal.Conexao();
        conexao.conecta();
        try {
            conexao.executeSelect(sql);
            while (conexao.resultset.next()) {
                 cboContato.addItem(conexao.resultset.getString("nome"));
            }
        } catch (java.sql.SQLException ex) {
            System.out.println("Erro no preechimento do Combobox");
        }
        conexao.desconecta();
    }
    
    @Override
    public void ativaCamposAdicionar(boolean b) {
        txtCompromisso.setEnabled(b);
        txtData.setEnabled(b);
        txtHora.setEnabled(b);
        txtEndereco.setEnabled(b);
        txtDescricao.setEnabled(b);
        chbContato.setEnabled(b);
        chbContato.setSelected(false);
        cboContato.setEnabled(false);
        if (b)
            txtCompromisso.requestFocus();
        ativaRadioButton();
    }
    
    @Override
    public void limpaAdicionar() {
        txtCompromisso.setText("");
        txtData.setText("");
        txtHora.setText("");
        txtEndereco.setText("");
        txtDescricao.setText("");
        cboContato.setSelectedIndex(0);
    }
    
    @Override
    public Compromisso getTela() {
        int codigoContato = -1;
        
        if (chbContato.isSelected())
            codigoContato = (new ContatoDAL().buscaIdProNome(cboContato.getSelectedItem().toString()));
        
        return new Compromisso(codigo, txtCompromisso.getText(), codigoContato,
                framework.utilitarios.Data.retornaData(txtData.getText(), txtHora.getText()),
                txtEndereco.getText(), txtDescricao.getText()
                );
    }
    
    @Override
    public void setTela(Object o) {
        Compromisso com = (Compromisso) o;
        codigo = com.getIdentifier();
        txtCompromisso.setText(com.getCompomisso());
        if (com.getCodigoContato() != 0) {
            chbContato.setSelected(true);
            cboContato.setSelectedItem(new ContatoDAL().buscaNomePorId(com.getCodigoContato()));
            cboContato.setEnabled(true);
        }
        txtData.setText(framework.utilitarios.Data.retornaStirng(com.getData()));
        txtHora.setText(horaFormatada.format(com.getData()));
        txtEndereco.setText(com.getEndereco());
        txtDescricao.setText(com.getDesc());
    }
    
    @Override
    public java.awt.event.ActionListener criarActionListener(framework.agendaFactory.lib.InternalFrame internalFrame) {
        CompromissoButtonListener buttonListener = new CompromissoButtonListener(internalFrame);
        return buttonListener;
    }

    @Override
    public javax.swing.JPanel[] criaPanel(java.awt.event.ActionListener listener) {
        javax.swing.JPanel[] panel = new javax.swing.JPanel[3];

        panel[0] = new javax.swing.JPanel();
        panel[1] = new javax.swing.JPanel();
        panel[2] = new javax.swing.JPanel();
        groupVisualizar = new javax.swing.ButtonGroup();
        
        //<editor-fold defaultstate="collapsed" desc=" Cria o panel para a aba de Listagem ">
        /**
         * Cria o panel da aba de listagem
         */
        javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
        javax.swing.JLabel lblVisualizar = new javax.swing.JLabel();
        javax.swing.JSeparator jSeparator8 = new javax.swing.JSeparator();
        rbnConcluido = new javax.swing.JRadioButton();
        rbnDia = new javax.swing.JRadioButton();
        rbnNaoConcluido = new javax.swing.JRadioButton();
        rbnSeleciona = new javax.swing.JRadioButton();
        dtCalendario = new com.toedter.calendar.JDateChooser();

        tabela.setAutoCreateRowSorter(true);
        tabela.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "", "Compromisso", "Data", "Hora", "Locar", "Contato"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                true, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabela.setMaximumSize(new java.awt.Dimension(2147483647, 2147483647));
        tabela.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabela);
        

        lblVisualizar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lblVisualizar.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblVisualizar.setText("Visualizar: ");
        
        groupVisualizar.add(rbnDia);
        rbnDia.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        rbnDia.setSelected(true);
        rbnDia.setText("Dia " + framework.utilitarios.Data.retonaString());
        rbnDia.addActionListener(listener);
        rbnDia.setName("rbnDia");

        groupVisualizar.add(rbnConcluido);
        rbnConcluido.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        rbnConcluido.setText("Concluidos");
        rbnConcluido.addActionListener(listener);
        rbnConcluido.setName("rbnConcluido");

        groupVisualizar.add(rbnNaoConcluido);
        rbnNaoConcluido.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        rbnNaoConcluido.setText("Não Concluidos");
        rbnNaoConcluido.addActionListener(listener);
        rbnNaoConcluido.setName("rbnNaoConcluido");
        
        groupVisualizar.add(rbnSeleciona);
        rbnSeleciona.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        rbnSeleciona.setText("Selecione uma data");
        rbnSeleciona.addActionListener(listener);
        rbnSeleciona.setName("rbnSeleciona");
        
        dtCalendario.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        dtCalendario.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                if (rbnSeleciona.isSelected()&& dtCalendario.getDate() != null) 
                    preencheTabela("SELECT * FROM compromisso WHERE datahora LIKE '" + framework.utilitarios.Data.retona(dtCalendario.getDate()) + "%' ORDER BY datahora");
            }
        });
        dtCalendario.setName("dtCalendario");

        javax.swing.GroupLayout panelContatosLayout = new javax.swing.GroupLayout(panel[0]);
        panel[0].setLayout(panelContatosLayout);
        panelContatosLayout.setHorizontalGroup(
            panelContatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelContatosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelContatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator8)
                    .addComponent(jScrollPane1)
                    .addGroup(panelContatosLayout.createSequentialGroup()
                        .addComponent(lblVisualizar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rbnDia)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(rbnConcluido)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rbnNaoConcluido)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(rbnSeleciona)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dtCalendario, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 92, Short.MAX_VALUE)))
                .addContainerGap())
        );
        panelContatosLayout.setVerticalGroup(
            panelContatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelContatosLayout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(panelContatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(dtCalendario, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(panelContatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblVisualizar)
                        .addComponent(rbnDia)
                        .addComponent(rbnConcluido)
                        .addComponent(rbnNaoConcluido)
                        .addComponent(rbnSeleciona)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator8, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE)
                .addContainerGap())
        );
        //</editor-fold>

        //<editor-fold defaultstate="collapsed" desc=" Cria o panel para a aba de Cadastro ">

        /**
         * Cria a aba de cadastro
         */
        javax.swing.text.MaskFormatter data = null, hora = null;

        javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel4 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel14 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel5 = new javax.swing.JLabel();
        javax.swing.JSeparator jSeparator2 = new javax.swing.JSeparator();
        javax.swing.JSeparator jSeparator3 = new javax.swing.JSeparator();
        javax.swing.JScrollPane jScrollPane2 = new javax.swing.JScrollPane();
        
        txtCompromisso = new javax.swing.JTextField();
        txtEndereco = new javax.swing.JTextField();
        //<editor-fold defaultstate="collapsed" desc=" Adicionando a mascara de data ao componete. (dd/mm/AAAA) ">
        try {
            data = new javax.swing.text.MaskFormatter("##/##/####");
        } catch (Exception erro) {
            JOptionPane.showMessageDialog(null, "Não foi possivel setar a mascara.\n\n\tErro: " + erro);
        }
        //</editor-fold>
        txtData = new javax.swing.JFormattedTextField(data);
        //<editor-fold defaultstate="collapsed" desc=" Adicionando a mascara de hora ao componete. (hh:mm) ">
        try {
            hora = new javax.swing.text.MaskFormatter("##:##");
        } catch (Exception erro) {
            JOptionPane.showMessageDialog(null, "Não foi possivel setar a mascara.\n\n\tErro: " + erro);
        }
        //</editor-fold>
        txtHora = new javax.swing.JFormattedTextField(hora);
        
        txtDescricao = new javax.swing.JEditorPane();
        chbContato = new javax.swing.JCheckBox();
        cboContato = new javax.swing.JComboBox();
        
        javax.swing.JPanel jPanel = new javax.swing.JPanel();

        txtCompromisso.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txtCompromisso.setToolTipText("Compromisso");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Compromisso: ");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Adicionar um novo Compromisso a agenda.");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Endereço: ");

        txtEndereco.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txtEndereco.setToolTipText("Esdereço");

        jLabel14.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel14.setText("Data: ");

        txtData.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtData.setToolTipText("Data");
        txtData.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Descrição: "));
        jScrollPane2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        txtDescricao.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txtDescricao.setToolTipText("Descrição do Comprimisso (não obrigratodio)");
        txtDescricao.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        jScrollPane2.setViewportView(txtDescricao);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Hora: ");

        txtHora.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txtHora.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtHora.setToolTipText("Hora (17:50)");

        chbContato.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        chbContato.setText("Contato: ");
        chbContato.setName("chbContato");
        chbContato.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cboContato.setEnabled(chbContato.isSelected());
            }
        });

        cboContato.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        cboContato.setMaximumSize(new java.awt.Dimension(200, 30));
        cboContato.setMinimumSize(new java.awt.Dimension(200, 30));
        cboContato.setPreferredSize(new java.awt.Dimension(200, 30));
        
        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("Categoria: ");
        
        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel);
        jPanel.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(chbContato)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cboContato, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 551, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, 482, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 292, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator2)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 541, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtCompromisso, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addComponent(jLabel14)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtData, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtHora, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(0, 200, Short.MAX_VALUE)))
                        .addContainerGap())))
        );
        
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCompromisso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chbContato)
                    .addComponent(cboContato, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14)
                    .addComponent(txtData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(33, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout panelAdicionarLayout = new javax.swing.GroupLayout(panel[1]);
        panel[1].setLayout(panelAdicionarLayout);
        panelAdicionarLayout.setHorizontalGroup(
                panelAdicionarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelAdicionarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap()));
        panelAdicionarLayout.setVerticalGroup(
                panelAdicionarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panelAdicionarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap()));
        //</editor-fold>

        //<editor-fold defaultstate="collapsed" desc=" Cria o panel para a aba de Grupos ">
        javax.swing.JScrollPane jScrollPane3 = new javax.swing.JScrollPane();
        javax.swing.JPanel jPanelGroup = new javax.swing.JPanel();
        javax.swing.JSeparator jSeparator1 = new javax.swing.JSeparator();

        tabelaCategoria.setAutoCreateRowSorter(true);
        tabelaCategoria.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        tabelaCategoria.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Categoria"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(tabelaCategoria);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("Clique em uma Categoria para ver os compromissos desta.");
        
        javax.swing.GroupLayout jPanelGroupLayout = new javax.swing.GroupLayout(jPanelGroup);
        jPanelGroup.setLayout(jPanelGroupLayout);
        jPanelGroupLayout.setHorizontalGroup(
            jPanelGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addGroup(jPanelGroupLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 541, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(210, Short.MAX_VALUE))
        );
        jPanelGroupLayout.setVerticalGroup(
            jPanelGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelGroupLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
                .addContainerGap())
        );
        
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(panel[2]);
        panel[2].setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelGroup, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelGroup, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        //</editor-fold>
        
        return panel;
    }
    
    @Override
    public JTable getTabelaLista() {
        return tabela;
    }
    
    private void tabelaMouseClicked(java.awt.event.MouseEvent evt) {
        if (evt.getClickCount() == 1){ 
            int linha, coluna;
            linha = tabela.getSelectedRow();
            coluna = tabela.getSelectedColumn();
            
            Compromisso compromisso = new Compromisso();
            compromisso.setCompomisso(tabela.getValueAt(linha, 1).toString());
            compromisso.setData(
                    framework.utilitarios.Data.retornaData(
                    tabela.getValueAt(linha, 2).toString(),
                    tabela.getValueAt(linha, 3).toString())
            );
            
            if (coluna == 0) {
                String confirma = "";
                if ((boolean)tabela.getValueAt(linha, 0))
                    confirma = "Marcar a atividade: \n\n\t" + tabela.getValueAt(linha, 1).toString() + "\n\nComo concluida?";
                else 
                    confirma = "Desmarcar a atividade: \n\n\t" + tabela.getValueAt(linha, 1).toString();
                 
                int opcao = JOptionPane.showConfirmDialog(null,confirma,"Confirmação de Atividade",JOptionPane.YES_NO_OPTION);
                if (opcao == JOptionPane.YES_OPTION) {
                    compromisso = (Compromisso) new CompromissoDAL().buscaAlterar(compromisso);
                    new CompromissoDAL().altetarStatus(compromisso.getIdentifier(), (!compromisso.getStatus()) );
                    preencheTabela(sql);
                } else {
                    tabela.setValueAt(!(boolean)tabela.getValueAt(linha, 0), linha, 0);
                }
            } else {
                limpaAdicionar();

                this.setTela(new CompromissoDAL().buscaAlterar(compromisso));
                ativaCamposAdicionar(false);
            }
            
        }
    }
    
    public void ativaRadioButton() {
        dtCalendario.setEnabled(rbnSeleciona.isSelected());
        dtCalendario.getDateEditor().setEnabled(false);
    }

    @Override
    public String getSQL() {
        return sql;
    }

    @Override
    public void setSQL(String sql) {
        this.sql = sql;
    }

    @Override
    public GroupStrategy getStrategy() {
        return new agenda.calendario.CompromissoStrategy();
    }

    @Override
    public void rePaintGroupsA() {
        super.rePaintGroupsA(); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Integer getGroupSelectedA() {
        return super.getGroupSelectedA(); //To change body of generated methods, choose Tools | Templates.
    }
    
    public void preecheTabelaCategoria() {
        tabelaCategoria.getColumnModel().getColumn(0).setPreferredWidth(150);
        
        javax.swing.table.DefaultTableModel modelo = (javax.swing.table.DefaultTableModel) tabelaCategoria.getModel();
        modelo.setNumRows(0);
        
        framework.dal.Conexao conexao = new framework.dal.Conexao();
        conexao.conecta();
        try {
            conexao.executeSelect(" SELECT nome FROM grupo ORDER BY nome ");
            String s = "";
            while (conexao.resultset.next()) {
                s = conexao.resultset.getString("nome");
                if (s.equals("!!!")) 
                    s = "Grupo Padrao";
                modelo.addRow(new Object[]{s});
            }
        } catch (java.sql.SQLException ex) {
            System.out.println("Pau...\nColocar mensagem de erro em: preencheTabela() de CompromissoFectory");
        }
        conexao.desconecta();
    }
}
